<?php		
/*
**********************************************
JoomLeague Next-Match Module
Copyright (c) 2007 JoomLeague.de
**********************************************
This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* NEXT MATCH MODULE
*
* 2007-08-03 - Version 0.91
*
* Module for displaying Next Match of the favoured
* JoomLeague Team in a selected Project 
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JoomLeague at:
http://http://www.joomleague.de//
**********************************************
*/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

if (file_exists($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php')) {
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php');
} 
else{
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/english.php');
}

require_once ($mosConfig_absolute_path.'/administrator/components/com_joomleague/joomleague.class.php');

$params = mosParseParams( $module->params );
$p = $params->project; //default 0
$show_pics = $params->show_picture; //default 0
$show_link= $params->show_link; //default 0
$show_next_first= $params->show_next_first;
$team= $params->team;
$use_fav= $params->use_fav;
$project = new JoomLeague( $database );
$project->load($p);
$thistime=$project->getTimestamp('',1);
if ($use_fav==1) $favteam = $project->fav_team;
else $favteam = $team;
$favtemp=explode(",", $favteam);
$counter = 1;
$anzahl_favs = 0;
$lastdate = '';
$lasttime = '';
$lastname = '';

//foreach($favtemp as $var) {
  $query = "SELECT m.match_date AS datum,
                   r.*, m.matchpart2 AS away,
                   m.matchpart1 AS home,
                   m.match_id AS machid, 
                   m.*
              FROM #__joomleague_matches m
         LEFT JOIN #__joomleague_rounds r ON m.round_id = r.id
	         WHERE ( m.matchpart1 IN ($favteam) OR m.matchpart2 IN ($favteam)) AND m.project_id = $p AND DATE_ADD(m.match_date, INTERVAL '".($project->game_regular_time + ($project->halftime * ($project->game_parts - 1)) + 1)."' MINUTE) > '".date('Y-m-d H:i:s',$thistime)."' ORDER by m.match_date, datum";

  $database->setQuery($query);
  $matches = $database->loadObjectList();

//if ($params->show_season_end_notice == "0") {
  if ((count($matches) > 0) && ($params->show_season_end_notice == "0")) {
//for ($counter=0;$counter<count($rounds);$counter++)
//{
  foreach($matches as $match) {
	$team1 = new JL_Team($database);
    $team1->load($match->home);
    $team2 = new JL_Team($database);
    $team2->load($match->away);
    
    if (in_array($match->home, $favtemp)) $anzahl_favs++;
    if (in_array($match->away, $favtemp)) $anzahl_favs++;
    
    if ($show_pics==1) {
      $query = sprintf("SELECT logo_middle FROM #__joomleague_clubs clubs
                     LEFT JOIN #__joomleague_teams teams ON clubs.id = teams.club_id
                         WHERE teams.id = %s", $match->home);
      $database->setQuery($query);
      $matchpart1_pic = $database->loadResult();
  
      $query = sprintf("SELECT logo_middle FROM #__joomleague_clubs clubs
                     LEFT JOIN #__joomleague_teams teams ON clubs.id = teams.club_id
                         WHERE teams.id = %s", $match->away);
  	  $database->setQuery($query);
  	  $matchpart2_pic = $database->loadResult();
  
  	  $matchpart1_pic = (trim($matchpart1_pic) != "")? sprintf('<img src="%s" alt="%s"/>', $matchpart1_pic, $team1->name) : "";
  	  $matchpart2_pic = (trim($matchpart2_pic) != "")? sprintf('<img src="%s" alt="%s"/>', $matchpart2_pic, $team2->name) : "";
	}
    if ($params->names == "0") {
      $nametype1 = $team1->short_name;
      $nametype2 = $team2->short_name;
    }
    if ($params->names == "1") {
      $nametype1 = $team1->middle_name;
      $nametype2 = $team2->middle_name;
    }
    if ($params->names == "2") {
      $nametype1 = $team1->name;
      $nametype2 = $team2->name;
    }
?>
<table cellspacing="1" cellpadding="1" border="0" width="100%">
<?php
    if (($params->show_project == '1') && ($lastname<>$project->name)) {
?>
		<tr>
      <td colspan="3" align="center"><b><? echo $project->name;?></b></td>
    </tr>
<?php
    }
?>
		<tr>
      <td colspan="3" align="center"><b><? if ($lastdate<>mosFormatDate($match->datum,_JL_MODULE_NEXT_MATCH_DAYDATE,0)) echo mosFormatDate($match->datum,_JL_MODULE_NEXT_MATCH_DAYDATE,0)."<br/>"; if (($lastdate<>mosFormatDate($match->datum,_JL_MODULE_NEXT_MATCH_DAYDATE,0)) || ($lasttime<>mosFormatDate($match->datum,"%H:%M",0))) echo mosFormatDate($match->datum,"%H:%M",0)."&nbsp;"._JL_CLOCK;?></b></td>
    </tr>
    <tr>
    	<td align="center" width="49%"><? if ($show_pics==1) echo $matchpart1_pic."<br/>";
    	echo $nametype1;?></td>
    	<td align="center">:</td>
    	<td align="center" width="49%"><? if ($show_pics==1) echo $matchpart2_pic."<br/>";
    	echo $nametype2;?></td>
    </tr>
</table>
<center>
<?
    if ($show_link==1) { ?>
<a href="<?php echo sefRelToAbs("index.php?option=com_joomleague&amp;func=showNextMatch&amp;p=$project->id&amp;mid=".$match->machid."&amp;pics=$show_pics&amp;Itemid=$Itemid");?>"><?php echo _JL_MODULE_NEXT_MATCH_STATISTIC;?></a>
<?php };?>
</center>
<?php
    if ($show_next_first==1) {
      $query = "SELECT DATE_ADD(r.round_date, INTERVAL '".$project->serveroffset."' HOUR_MINUTE) AS datum,
                       r.*,
                       m.matchpart2_result AS away,
                       m.matchpart1_result AS home,
                       m.match_id AS machid,
                       m.* 
                  FROM #__joomleague_matches m, #__joomleague_rounds r
                 WHERE m.round_id = r.id AND
                       ( m.matchpart1 = $team2->id AND
                         m.matchpart2 = $team1->id) AND
                       r.round_date < NOW() ORDER by r.round_date DESC LIMIT 1";

	  $database->setQuery($query);
	  $roundsnext = $database->loadObjectList();
	  if ($roundsnext) {
?><center><?php echo _JL_MODULE_NEXT_MATCH_FIRST_MATCH;?>&nbsp;<? echo mosFormatDate($roundsnext[0]->datum,"(_".JL_DAYDATE.")",0);?><br /><? echo "$nametype1 - $nametype2  &nbsp;&nbsp;<b>".$roundsnext[0]->home.":".$roundsnext[0]->away."</b></center>";
	  }
    }
    if ($anzahl_favs >= count($favtemp)) break;
    if ($counter<count($matches)) {
?>
<hr align="center" width="95%" />
<?php
    }
    $lastname = $project->name;
    $lastdate = mosFormatDate($match->datum,_JL_MODULE_NEXT_MATCH_DAYDATE,0);
    $lasttime = mosFormatDate($match->datum,"%H:%M",0);
    $counter++;
  }
} else echo $params->season_end_notice;

?>
